﻿using System;
using Microsoft.AspNetCore.Mvc;
using DataBaseConnect.App;
using Utils;
using Entity;
using System.Collections.Generic;
using Entity.Dto;
using Microsoft.AspNetCore.Html;
using System.Text;
using System.Linq;
using DataBaseConnect.IRepositories;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class ButtonController : BaseController
    {
        #region 构造实例化

        public IButtonService _buttonService;
        public IButtonRepository _buttonRepository;

        public ButtonController(IButtonService buttonService, IButtonRepository buttonRepository)
        {
            _buttonService = buttonService;
            _buttonRepository = buttonRepository;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增按钮页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddButton()
        {
            var list = _buttonService.GetButtonStyleList("");
            return View(list);
        }

        /// <summary>
        /// 编辑按钮页
        /// </summary>
        /// <param name="ButtonId">按钮id</param>
        /// <returns></returns>
        public IActionResult EditButton(int ButtonId)
        {
            ButtonOrButtonStyleDto item = new ButtonOrButtonStyleDto();
            var model = _buttonService.GetAllButtonByButtonIdMsg(ButtonId);
            if (ButtonId != 0)
            {
                item.ButtonName = model.ButtonName;
                item.ButtonKey = model.ButtonKey;
                item.ButtonId = model.ButtonId;
                item.ButtonStyleId = model.ButtonStyleId;
                item.OrderBy = model.OrderBy;
                item.ButtonStyleList = _buttonService.GetButtonStyleList("");
            }
            return View(item);
        }

        /// <summary>
        /// 新增按钮样式页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddButtonStyle()
        {
            return View();
        }

        /// <summary>
        /// 编辑按钮样式页
        /// </summary>
        /// <param name="ButtonStyleId">按钮样式id</param>
        /// <returns></returns>
        public IActionResult EditButtonStyle(int ButtonStyleId)
        {
            var item = _buttonService.GetButtonStyleIdMsg(ButtonStyleId);
            return View(item);
        }

        /// <summary>
        /// 按钮权限设置页
        /// </summary>
        /// <returns></returns>
        public IActionResult SetButtonMenu()
        {
            return View();
        }

        /// <summary>
        /// 根据菜单id查找按钮权限
        /// </summary>
        /// <param name="MenuId"></param>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public string ButtonRole(int MenuId, int RoleId)
        {
            HtmlString html = new HtmlString("");
            StringBuilder sb = new StringBuilder();
            List<ButtonDto> buttonDto = new List<ButtonDto>();
            var buttonRoel = _buttonService.GetButtonRoleByMenuIdList(MenuId, RoleId);
            foreach (var item in buttonRoel)
            {
                var buttonStyleId = _buttonService.GetAllButtonByButtonIdMsg(item.ButtonId);
                var style = _buttonService.GetButtonStyleIdMsg(buttonStyleId.ButtonStyleId);
                buttonDto.Add(new ButtonDto
                {
                    ButtonId = buttonStyleId.ButtonId,
                    ButtonName = buttonStyleId.ButtonName,
                    ButtonKey = buttonStyleId.ButtonKey,
                    ButtonStyleId = buttonStyleId.ButtonStyleId,
                    MenuId = item.MenuId,
                    StyleCss = style.Style + " " + style.Size + " " + style.Types,
                    ThemeName = style.ButtonStyleName,
                    Iocn = style.Icon,
                    OrderBy = buttonStyleId.OrderBy
                });
            }
            buttonDto = buttonDto.OrderBy(f => f.OrderBy).ToList();
            if (buttonDto.Count != 0)
            {
                foreach (var item in buttonDto)
                {
                    if (!string.IsNullOrEmpty(item.Iocn))
                    {
                        html = new HtmlString($" <button id='" + item.ButtonKey + "' class='" + item.StyleCss + "'><i class='layui-icon'>" + item.Iocn + "</i> " + item.ButtonName + "</button>");
                    }
                    else
                    {
                        html = new HtmlString($" <button id='" + item.ButtonKey + "' class='" + item.StyleCss + "'>" + item.ButtonName + "</button>");
                    }
                    sb.Append(html.Value);
                }
            }
            else
            {
                html = new HtmlString($" <span>还没有操作按钮，快去添加吧 :)</span>");
                sb.Append(html.Value);
            }
            return sb.ToString();
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取所有按钮样式&&查询
        /// </summary>
        /// <param name="ButtonStyleName">按钮样式名称</param>
        /// <returns></returns>
        public JsonResult GetButtonStyleList(string ButtonStyleName)
        {
            ReceiveStatus<ButtonStyle> receiveStatus = new ReceiveStatus<ButtonStyle>();
            try
            {
                var list = _buttonService.GetButtonStyleList(ButtonStyleName);
                receiveStatus.msg = "获取成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 新增按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public JsonResult AddButtonStyleMsg(ButtonStyle Input)
        {
            Input.Icon = Input.Icon == null ? "" : Input.Icon;
            Input.Size = Input.Size == null ? "" : Input.Size;
            Input.Types = Input.Types == null ? "" : Input.Types;
            return Json(_buttonService.AddButtonStyleMsg(Input));
        }

        /// <summary>
        /// 编辑按钮样式信息
        /// </summary>
        /// <param name="Input">按钮样式实体</param>
        /// <returns></returns>
        public JsonResult EditButtonStyleMsg(ButtonStyle Input)
        {
            Input.Icon = Input.Icon == null ? "" : Input.Icon;
            Input.Size = Input.Size == null ? "" : Input.Size;
            Input.Types = Input.Types == null ? "" : Input.Types;
            return Json(_buttonService.EditButtonStyleMsg(Input));
        }

        /// <summary>
        /// 新增按钮信息
        /// </summary>
        /// <param name="Input">按钮信息实体</param>
        /// <returns></returns>
        public JsonResult AddButtonMsg(Button Input)
        {
            return Json(_buttonService.AddButtonMsg(Input));
        }

        /// <summary>
        /// 编辑按钮信息
        /// </summary>
        /// <param name="Input">按钮信息实体</param>
        /// <returns></returns>
        public JsonResult EditButtonMsg(Button Input)
        {
            return Json(_buttonService.EditButtonMsg(Input));
        }

        /// <summary>
        /// 根据菜单id和角色id获取对应的按钮信息
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public JsonResult GetButtonByMenuIdList(int MenuId, int RoleId)
        {
            List<ButtonDto> buttonDto = new List<ButtonDto>();
            var button = _buttonService.GetAllButtonMenuIdList(MenuId);
            var buttonRoel = _buttonService.GetButtonRoleByMenuIdList(MenuId, RoleId);
            foreach (var item in button)
            {
                var style = _buttonService.GetButtonStyleIdMsg(item.ButtonStyleId);
                buttonDto.Add(new ButtonDto
                {
                    ButtonId = item.ButtonId,
                    ButtonName = item.ButtonName,
                    ButtonKey = item.ButtonKey,
                    ButtonStyleId = item.ButtonStyleId,
                    MenuId = item.MenuId,
                    StyleCss = style.Style + " " + style.Size + " " + style.Types,
                    ThemeName = style.ButtonStyleName,
                    Iocn = style.Icon,
                    OrderBy = item.OrderBy
                });
            }
            foreach (var item in buttonRoel)
            {
                foreach (var data in buttonDto)
                {
                    if (item.ButtonId == data.ButtonId)
                    {
                        data.Checked = "checked='checked'";
                    }
                }
            }
            return Json(buttonDto);
        }

        /// <summary>
        /// 设置菜单按钮权限
        /// </summary>
        /// <param name="ButtonRole">按钮信息集合</param>
        /// <param name="MenuId">菜单id</param>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public JsonResult SetButtonRoleMsg(List<ButtonRole> ButtonRole, int MenuId, int RoleId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                receiveStatus = _buttonRepository.DelButtonRoleMsg(f => f.MenuId == MenuId && f.RoleId == RoleId);
                if (ButtonRole.Count != 0)
                {
                    receiveStatus = _buttonRepository.SetButtonRoleMsg(ButtonRole);
                }
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "失败：" + ex.Message;
            }

            return Json(receiveStatus);
        }

        #endregion
    }
}
